package tbalert.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

import tbalert.hibernate3.Finder;
import tbalert.session.SessionProvider;
import tbalert.web.SysConstants;
import tbalert.web.entity.BaseInfo;
import tbalert.web.entity.Dwdm;
import tbalert.web.manager.BrxxMng;
import tbalert.web.view.ViewBrxxExcel;
import tbalert.web.view.ViewJzkExcel;
import tbalert.web.view.ViewYmjgExcel;

@Controller
@RequestMapping(value = "/xls")
public class ReportXlsAct {

	@RequestMapping(value = "/brxx.do")
	public ModelAndView viewExcel(ModelMap model, HttpServletRequest request,
			BaseInfo baseInfo, String hqlCondition, String djrqq, String djrqz) {
		Dwdm dwdm = (Dwdm) session.getAttribute(request, SysConstants.DWDM_KEY);
		String sjdwId = dwdm.getDwid();
		if (!StringUtils.isBlank(sjdwId)
				&& (sjdwId.equals("00") || sjdwId.equals("bssa"))) {
			sjdwId = "";
		}

		Finder finder = Finder
				.create("from BaseInfo b, Blxxk k, Fxpgb f where b.kicid=k.kicid and b.kicid=f.kicid and b.djdw like '"
						+ sjdwId + "%'");
		if (!StringUtils.isBlank(hqlCondition)) {
			finder.append(hqlCondition);
		} else {
			if (!StringUtils.isBlank(baseInfo.getKicid())) {
				finder.append(" and b.kicid like '%" + baseInfo.getKicid() + "%'");
			}
			if (!StringUtils.isBlank(baseInfo.getDjhm())) {
				finder.append(" and b.djhm like '%" + baseInfo.getDjhm() + "%'");
			}
			if (!StringUtils.isBlank(baseInfo.getBrxm())) {
				finder.append(" and b.brxm like '%" + baseInfo.getBrxm() + "%'");
			}
			if (!StringUtils.isBlank(djrqq)) {
				finder.append(" and b.djrq >= '" + djrqq + "'");
			}
			if (!StringUtils.isBlank(djrqz)) {
				finder.append(" and b.djrq <= '" + djrqz + "'");
			}
			if (baseInfo.getDjdw() != null && !StringUtils.isBlank(baseInfo.getDjdw().getDwid())) {
				finder.append(" and b.djdw like '" + baseInfo.getDjdw().getDwid() + "%'");
			}
		}
		finder.append(" and b.submit=1 order by b.djdw,b.kicid");

		List<BaseInfo> baseInfoList = brxxMng.find(finder);

		ViewBrxxExcel viewBrxxExcel = new ViewBrxxExcel();
		viewBrxxExcel.setDownloadFileName("病人信息表");

		model.put("baseInfoList", baseInfoList);
		return new ModelAndView(viewBrxxExcel);
	}

	@RequestMapping(value = "/ymjg.do")
	public ModelAndView viewYmjgExcel(ModelMap model,
			HttpServletRequest request, String kicid, String djrqq,
			String djrqz, String ym_inh, String ym_rfp, String ym_emb,
			String ym_sm) {
		Dwdm dwdm = (Dwdm) session.getAttribute(request, SysConstants.DWDM_KEY);
		String sjdwId = dwdm.getDwid();
		if (!StringUtils.isBlank(sjdwId)
				&& (sjdwId.equals("00") || sjdwId.equals("bssa"))) {
			sjdwId = "";
		}

		Finder finder = Finder
				.create("from BaseInfo b, Ymjg y where b.kicid=y.kicid and b.djdw like '"
						+ sjdwId + "%'");
		if (!StringUtils.isBlank(kicid)) {
			finder.append(" and b.kicid like '%" + kicid + "%'");
		}
		if (!StringUtils.isBlank(djrqq)) {
			finder.append(" and b.djrq >= '" + djrqq + "'");
		}
		if (!StringUtils.isBlank(djrqz)) {
			finder.append(" and b.djrq <= '" + djrqz + "'");
		}

		String hql = "";
		if (!StringUtils.isBlank(ym_inh)) {
			hql = "(y.ym_inh = '" + ym_inh + "')";
		}
		if (!StringUtils.isBlank(ym_rfp)) {
			if (!StringUtils.isBlank(hql)) {
				hql = hql.substring(0, hql.length() - 1) + " or y.ym_rfp = '"
						+ ym_rfp + "')";
			} else {
				hql = "(y.ym_rfp = '" + ym_rfp + "')";
			}
		}
		if (!StringUtils.isBlank(ym_emb)) {
			if (!StringUtils.isBlank(hql)) {
				hql = hql.substring(0, hql.length() - 1) + " or y.ym_emb = '"
						+ ym_emb + "')";
			} else {
				hql = "(y.ym_emb = '" + ym_emb + "')";
			}
		}
		if (!StringUtils.isBlank(ym_sm)) {
			if (!StringUtils.isBlank(hql)) {
				hql = hql.substring(0, hql.length() - 1) + " or y.ym_sm = '"
						+ ym_sm + "')";
			} else {
				hql = "(y.ym_sm= '" + ym_sm + "')";
			}
		}
		if (!StringUtils.isBlank(hql)) {
			hql = " and " + hql;
		}
		finder.append(hql + " and b.submit=1");

		List<BaseInfo> baseInfoList = brxxMng.find(finder);
		ViewYmjgExcel viewYmjgExcel = new ViewYmjgExcel();
		viewYmjgExcel.setDownloadFileName("药敏结果报告表");
		
		model.put("baseInfoList", baseInfoList);
		return new ModelAndView(viewYmjgExcel);
	}
	
	@RequestMapping(value = "/jzk.do")
	public ModelAndView viewJzkExcel(ModelMap model, HttpServletRequest request,
			BaseInfo baseInfo, String hqlCondition, String djrqq, String djrqz) {
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		SessionFactory sf = (SessionFactory)ac.getBean("sessionFactory");
		List<BaseInfo> baseInfoList = null;
		Session hs = null;
		try{
			hs = sf.openSession();
			hs.beginTransaction();
			String hql = "from BaseInfo b where b.exportToJzk='0' and b.submit=1 order by b.djdw,b.kicid";
			baseInfoList = hs.createQuery(hql).list();
			String sql="update tb_baseinfo set exportToJzk='1' where exportToJzk='0' and submit=1";
			hs.createSQLQuery(sql).executeUpdate();
			hs.getTransaction().commit();
		}catch(HibernateException he){
			he.printStackTrace();
			return null;
		}

		ViewJzkExcel viewJzkExcel = new ViewJzkExcel();
		viewJzkExcel.setDownloadFileName("病人信息_药敏结果");

		model.put("baseInfoList", baseInfoList);
		return new ModelAndView(viewJzkExcel);
	}

	@Autowired(required = true)
	private BrxxMng brxxMng;
	@Autowired(required = true)
	private SessionProvider session;

}
